package com.authManager.server.conf;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

/**
 * 说明：统一接口异常处理
 * 
 * @author: 小旋风
 */
@ControllerAdvice
public class WebExceptionController {
	private static final Logger log = LoggerFactory.getLogger(WebExceptionController.class);
	@ExceptionHandler(Throwable.class)
	@ResponseBody
	public Object handleException(Throwable e, HttpServletRequest request, HttpServletResponse response) {
		try {
//			log.error("地址" + request.getRequestURI() + "异常的错误类：" + e.getClass() + " 错误信息是：" + e.getMessage()
//					+ "状态码是：" + response.getStatus());
			log.error("地址" + request.getRequestURI() + "异常的错误类：" + e.getClass() + " 错误信息是：" + e.getMessage()
			+ "状态码是：" + response.getStatus(), e);
//			e.printStackTrace();
			if (e instanceof MissingServletRequestParameterException) {
				MissingServletRequestParameterException ex = (MissingServletRequestParameterException) e;
				return new ResultData.Builder<Object>(ResultCode.unknown_error)
						.setMessage("缺少必要参数,参数名称为【" + ex.getParameterName() + "】").build();
			}
			if (e instanceof MethodArgumentTypeMismatchException) {
				MethodArgumentTypeMismatchException ex = (MethodArgumentTypeMismatchException) e;
				return new ResultData.Builder<Object>(ResultCode.unknown_error)
						.setMessage("参数类型不匹配,参数" + ex.getName() + "类型应该为" + ex.getRequiredType()).build();
			}
			if (e instanceof TypeMismatchException) {
				TypeMismatchException ex = (TypeMismatchException) e;
				return new ResultData.Builder<Object>(ResultCode.unknown_error)
						.setMessage("参数类型不匹配,参数" + ex.getPropertyName() + "类型应该为" + ex.getRequiredType()).build();
			}
			if (e instanceof HttpRequestMethodNotSupportedException) {
				HttpRequestMethodNotSupportedException ex = (HttpRequestMethodNotSupportedException) e;
				return new ResultData.Builder<Object>(ResultCode.unknown_error)
						.setMessage("不支持【 " + ex.getMethod() + " 】请求").build();
			}
			if (e instanceof ConstraintViolationException) {
				ConstraintViolationException ex = (ConstraintViolationException) e;
				Set<ConstraintViolation<?>> list = ex.getConstraintViolations();
				StringBuffer sb = new StringBuffer();
				for (ConstraintViolation<?> constraintViolation : list) {
					sb.append("【");
					sb.append(constraintViolation.getMessage());
					sb.append("】");
				}
				log.error("msg:" + sb.toString());
				return new ResultData.Builder<Object>(ResultCode.verifyParam).setMessage(sb.toString()).build();
			}
		} catch (Throwable e2) {
			log.error("WebExceptionController 类 handleException方法抛异常了",e2);
		}
		return new ResultData.Builder<Object>(ResultCode.unknown_error).build();
	}

}